<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Mine extends Admin_Controller {

    public function __construct() {
        parent::__construct();

        $this->username = $_COOKIE['username'];
        $this->adminname = 'admin';
        $this->load->model("admin/admin_model");
    }

    /**
    *   auto:xg
     * 个人资料
     */
	public function index()
	{
        $data['op'] = 'info';
        $data['userinfo'] = $this->admin_model->getUserinfo($this->username);
        $this->load->view("admin/mine",$data);
	}

    /**
     * auto:xg
     * 修改密码
     */
    public function editpass(){
        $data['username'] = $this->username;
        $data['op'] = 'editpass';
        $this->load->view("admin/mine",$data);
    }

    /**
     * auto:xg
     * 保存密码
     */
    public function savepass(){
        $username = $this->username;
        $pass_old = $this->input->post('pass_old');
        $pass_new = $this->input->post('pass_new');
        $pass_new2 = $this->input->post('pass_new2');

        $flag = $this->admin_model->check_info($username, $pass_old);
        if($flag == 2){
            if($pass_new !== $pass_new2){
                cpmessage("操作失败,两次密码输入不一致", "/index.php?d=admin&c=mine");
            }else{
                $flag = $this->admin_model->change_pass($this->username,$pass_new);
                if($flag>0){
                    cpmessage("操作成功", "/index.php?d=admin&c=mine");
                }else{
                    cpmessage("操作失败", "/index.php?d=admin&c=mine");
                }
            }
        }else{
            cpmessage("操作失败,旧密码错误", "/index.php?d=admin&c=mine");
        }
    }

    /**
     * auto:xg
     * 增加管理员
     */
    public function  addadmin(){
        $username = $this->username;
        $data['username'] = $this->username;
        if($username != $this->adminname){
            cpmessage("操作失败,权限不够", "/index.php?d=admin&c=mine");
        }
        $data['op'] = 'addadmin';
        $this->load->view("admin/mine",$data);
    }

    /**
     * auto:xg
     * 保存管理员信息
     */
    public function saveadmin(){
        $post_date['username'] = $this->input->post('username') ? $this->input->post('username') : '';
        $post_date['password'] = $this->input->post('password') ? $this->input->post('password') : '';
        $post_date['password'] = $this->admin_model->make_password($post_date['password']);
        $post_date['nickname'] = $this->input->post('nickname') ? $this->input->post('nickname') : '';
        $post_date['photo'] = $this->input->post('photo') ? $this->input->post('photo') : '';
        $post_date['qq'] = $this->input->post('qq') ? $this->input->post('qq') : '';
        $post_date['phone'] = $this->input->post('phone') ? $this->input->post('phone') : '';
        $post_date['intro'] = $this->input->post('intro') ? $this->input->post('intro') : '';

        $flag = $this->admin_model->save_admin($post_date);
        if($flag>0){
            cpmessage("添加成功", "/index.php?d=admin&c=mine&m=adminlist");
        }else{
            cpmessage("添加失败", "/index.php?d=admin&c=mine&m=adminlist/");
        }
    }

    /**
     * auto:xg
     * 管理员列表
     */
    public function adminlist(){
        if($this->username != $this->adminname){
            cpmessage("操作失败,权限不够", "/index.php?d=admin&c=mine");
        }
        $data['admin_list'] = $this->admin_model->get_all_admin();
        $data['dis_arr'] = array('未禁用','<span style="color: red;">禁用</span>');
        $data['op'] = 'adminlist';
        $this->load->view("admin/mine",$data);
    }

    /**
     * auto:xg
     * 改变编辑状态
     */
    public function changdisable(){
        $flag = $this->input->get('flag') ? intval($this->input->get('flag')) : 0;
        $userid = $this->input->get('userid');
        if($this->username != $this->adminname){
            cpmessage("操作失败,权限不够", "/index.php?d=admin&c=mine");
        }
        if(!empty($userid)){
            $flag = $this->admin_model->chang_disable($flag,$userid);
            if($flag>0){
                cpmessage("操作成功", "/index.php?d=admin&c=mine&m=adminlist");
            }else{
                cpmessage("操作失败", "/index.php?d=admin&c=mine&m=adminlist");
            }
        }
    }

    /**
     * auto:xg
     * 管理员修改其他编辑密码
     */
    public function changpass_other(){
        $username = $this->input->get('username') ? $this->input->get('username') : '';
        if($this->username != $this->adminname || empty($username)){
            cpmessage("操作失败,权限不够", "/index.php?d=admin&c=mine");
        }else{
            $data['op'] = 'changpass_other';
            $data['username'] = $username;
            $this->load->view("admin/mine",$data);
        }


    }

    /**
     * auto:xg
     * 管理员修改其他账号密码，保存数据
     */
    public function savepass_other(){

        $username = $this->input->post('username');
        $pass_new = $this->input->post('pass_new');

        if(!empty($username)){
            $flag = $this->admin_model->change_pass($username,$pass_new);
            if($flag>0){
                cpmessage("操作成功", "/index.php?d=admin&c=mine&m=adminlist");
            }else{
                cpmessage("操作失败", "/index.php?d=admin&c=mine&m=adminlist");
            }
        }
    }
}
